package Tree_09.BinarySortTree_07;

/*
 * @Author 罗
 * @date 2020/3/26 - 2:49 上午
 *
 */

public class NodeForBinarySortTree {
    int value;
    NodeForBinarySortTree left;
    NodeForBinarySortTree right;

    /**
     * 中序遍历节点
     * @param node
     */
    public static void infixList(NodeForBinarySortTree node,int count){
        if(node == null){
            return;
        }
        count++;
        infixList(node.left,count);
        for (int i = 0; i < count; i++) {
            System.out.print("--");
        }
        System.out.println(node);
        infixList(node.right,count);
    }

    /**
     * 添加节点
     * @param node
     */
    public void add(NodeForBinarySortTree node){
        /*
        * 将要添加的节点和当前节点的值做比较
        *
        * */
        if(node.value < this.value){
            /*
            * 新添加的节点的value < 当前节点的value
            * */
            if(this.left == null){
                /*
                * 当前节点的左边没有连接任何节点
                * */
                this.left = node;
            }else{
                /*
                * 递归地将新添加的节点添加到本节点的左边下一个节点
                * */
                this.left.add(node);
            }
        }else{
            /*
            * 新添加的节点的value >= 当前节点的value
            * */
            if(this.right == null){
                this.right = node;
            }else{
                this.right.add(node);
            }
        }
    }

    @Override
    public String toString() {
        return "NodeForBinarySortTree{" +
                "value=" + value +
                '}';
    }

    public NodeForBinarySortTree(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public NodeForBinarySortTree getLeft() {
        return left;
    }

    public NodeForBinarySortTree getRight() {
        return right;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void setLeft(NodeForBinarySortTree left) {
        this.left = left;
    }

    public void setRight(NodeForBinarySortTree right) {
        this.right = right;
    }
}
